package com.winchain.chainbill.serfee.service.impl;

import com.shell.bee.acct.account.po.AcctBas;
import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.MoneyUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.cfca.CfcaConfiguration;
import com.shell.bee.cfca.paperless.request.CfcaSignRequest;
import com.shell.bee.cfca.paperless.response.BaseResponse;
import com.shell.bee.cfca.paperless.service.CfcaSignTxService;
import com.shell.bee.contract.ContConfiguration;
import com.shell.bee.contract.ContService;
import com.shell.bee.contract.ContTemplateHelper;
import com.shell.bee.contract.po.ContBas;
import com.shell.bee.file.po.PubFile;
import com.shell.bee.file.service.PubFileService;
import com.shell.bee.report.utils.FilePathHelper;
import com.shell.bee.report.utils.PdfUtils;
import com.shell.feign.PubAttTmplFeign;
import com.shell.feign.PubUserFeign;
import com.shell.framework.utils.CommonDataUtil;
import com.shell.framework.utils.SysDateUtil;
import com.shell.governor.modules.po.PubAttTmpl;
import com.shell.governor.user.po.PubUser;
import com.winchain.chainbill.basic.cus.po.CusRec;
import com.winchain.chainbill.basic.cus.service.CusRecService;
import com.winchain.chainbill.bill.base.po.CbDct;
import com.winchain.chainbill.bill.base.po.CbRec;
import com.winchain.chainbill.bill.base.service.CbDctService;
import com.winchain.chainbill.bill.base.service.CbRecService;
import com.winchain.chainbill.tbill.base.po.TbDct;
import com.winchain.chainbill.tbill.base.po.TbRec;
import com.winchain.chainbill.tbill.base.service.TbDctService;
import com.winchain.chainbill.tbill.base.service.TbRecService;
import com.winchain.chainbill.tbill.biz.contract.keyword.ServiceFeeDebitCertKeyword;
import com.winchain.chainbill.trade.service.BillAcctService;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ServiceFeeAttService {
	private static final String DATE_FORMAT = "yyyy��MM��dd��";
	private static final String FILE_PATH = "service-fee";
	private static final String FILE_NAME = "��������";
	private static final String REASON = "������������";
	private static final String DEBIT_CERT_KEY = "����";
	@Autowired
	private CbDctService cbDctService;
	@Autowired
	private CbRecService cbRecService;
	@Autowired
	private TbDctService tbDctService;
	@Autowired
	private TbRecService tbRecService;
	@Autowired
	private CusRecService cusRecService;
	@Autowired
	private PubAttTmplFeign pubAttTmplFeign;
	@Autowired
	private BillAcctService billAcctService;
	@Autowired
	private ContService contService;
	@Autowired
	private PubFileService pubFileService;
	@Autowired
	private PubUserFeign pubUserFeign;
	@Autowired
	private CfcaSignTxService cfcaSignTxService;

	public PubFile genDebitCert(String dctId, String bizType) {
		Map<String, Object> map;
		if ("0".equals(bizType)) {
			map = getBillDebitCertContent(dctId);
		} else {
			map = getTBillDebitCertContent(dctId);
		}
		String content = (String) map.get("content");
		String billId = (String) map.get("billId");
		String docName = SystemUtil.getUUID();
		String subDir = DateUtil.getSystemDate();
		String localPdfPath = ContConfiguration.getLocalPdfPath();
		if (StringUtil.emptyAndNull(localPdfPath)) {
			throw new UnCheckMsgException("������������������");
		}
		StringBuilder pathBuilder = new StringBuilder(localPdfPath);
		pathBuilder.append(File.separator).append("service-fee").append(File.separator).append(subDir);
		String pdfPath = pathBuilder.toString();
		try {
			PdfUtils.getPdfByContent(content, pdfPath, docName);
		} catch (IOException e) {
			e.printStackTrace();
			throw new UnCheckMsgException("����pdf������" + e.getMessage());
		}
		String localPdfFullPath = FilePathHelper.getFullPath(pdfPath, docName, "pdf");
		PubFile file = this.pubFileService.saveFile(SystemUtil.getUUID(), "��������", localPdfFullPath, "13");
		this.pubFileService.setRelateId(file.getFileId(), billId, file.getFileType());
		return file;
	}

	private Map<String, Object> getTBillDebitCertContent(String dctId) {
		TbDct dct = (TbDct) this.tbDctService.selectByKey(dctId);
		TbRec rec = (TbRec) this.tbRecService.selectByKey(dct.getBillId());
		PubAttTmpl attTmpl = this.pubAttTmplFeign.getPubAttTmplByTypeAndGrpId("2", rec.getGrpId());
		if (attTmpl == null) {
			throw new UnCheckMsgException("����������������������������������");
		}
		ServiceFeeDebitCertKeyword keyword = new ServiceFeeDebitCertKeyword();
		keyword.setDebitDate(SysDateUtil.parseDate(SysDateUtil.getSystemDate(), "yyyyMMdd", "yyyy��MM��dd��"));
		keyword.setBuyerNm(dct.getBuyerNm());
		AcctBas buyerAcct = this.billAcctService.getAcctBas(dct.getBuyerId(), null, "00");
		keyword.setBuyerAcctNo(buyerAcct.getAcctNo());
		keyword.setBuyerBankNm(buyerAcct.getFiNm());

		CusRec artisan = this.cusRecService.getArtisan(null);
		keyword.setSellerNm(artisan.getCusName());
		AcctBas sellerAcct = this.billAcctService.getAcctBas(artisan.getCusId(), null, "00");
		keyword.setSellerAcctNo(sellerAcct.getAcctNo());
		keyword.setSellerBankNm(sellerAcct.getFiNm());
		keyword.setBigAmt(MoneyUtil.number2CNMoney(dct.getSerFee()));
		keyword.setAmt(MoneyUtil.format(dct.getSerFee()));
		keyword.setBillCode(rec.getBillCode());

		ContBas cont = this.contService.getLatestCont(artisan.getCusId(), rec.getBillId(), "54", null);
		keyword.setContSn(cont.getContSn());

		keyword.setBankNm(dct.getFinNm());
		String content = ContTemplateHelper.replaceContent(attTmpl.getContent(), keyword);
		Map<String, Object> ret = new HashMap();
		ret.put("content", content);
		ret.put("artisan", artisan);
		ret.put("billId", rec.getBillId());
		return ret;
	}

	private Map<String, Object> getBillDebitCertContent(String dctId) {
		CbDct dct = (CbDct) this.cbDctService.selectByKey(dctId);
		CbRec rec = (CbRec) this.cbRecService.selectByKey(dct.getBillId());
		PubAttTmpl attTmpl = this.pubAttTmplFeign.getPubAttTmplByTypeAndGrpId("2", rec.getGrpId());
		if (attTmpl == null) {
			throw new UnCheckMsgException("����������������������������������");
		}
		ServiceFeeDebitCertKeyword keyword = new ServiceFeeDebitCertKeyword();
		keyword.setDebitDate(SysDateUtil.parseDate(SysDateUtil.getSystemDate(), "yyyyMMdd", "yyyy��MM��dd��"));
		keyword.setBuyerNm(dct.getBuyerNm());
		AcctBas buyerAcct = this.billAcctService.getAcctBas(dct.getBuyerId(), null, "00");
		keyword.setBuyerAcctNo(buyerAcct.getAcctNo());
		keyword.setBuyerBankNm(buyerAcct.getFiNm());

		CusRec artisan = this.cusRecService.getArtisan(null);
		keyword.setSellerNm(artisan.getCusName());
		AcctBas sellerAcct = this.billAcctService.getAcctBas(artisan.getCusId(), null, "00");
		keyword.setSellerAcctNo(sellerAcct.getAcctNo());
		keyword.setSellerBankNm(sellerAcct.getFiNm());
		keyword.setBigAmt(MoneyUtil.number2CNMoney(dct.getSerFee()));
		keyword.setAmt(MoneyUtil.format(dct.getSerFee()));
		keyword.setBillCode(rec.getBillCode());

		ContBas cont = this.contService.getLatestCont(artisan.getCusId(), rec.getBillId(), "18", null);
		keyword.setContSn(cont.getContSn());

		keyword.setBankNm(dct.getFinNm());
		String content = ContTemplateHelper.replaceContent(attTmpl.getContent(), keyword);
		Map<String, Object> ret = new HashMap();
		ret.put("content", content);
		ret.put("artisan", artisan);
		ret.put("billId", rec.getBillId());
		return ret;
	}

	public void signDebitCert(String billId, String finId, String finNm, String dctId, String bizType) {
		PubFile file = new PubFile();
		file.setRelateId(billId);
		file.setFileType("13");
		List<PubFile> files = this.pubFileService.selectFiles(file);
		if ((files == null) || (files.size() == 0)) {
			file = genDebitCert(dctId, bizType);
		} else {
			file = (PubFile) files.get(0);
		}
		String sealCode = CfcaConfiguration.getCfcaBankEtcCode();
		PubUser user = this.pubUserFeign.selectByKey(CommonDataUtil.getUserId());
		CfcaSignRequest request = new CfcaSignRequest();
		request.setUserId(user.getUserId());
		request.setUserName(user.getUserName());
		request.setIdNo(user.getCertNo());
		request.setPhoneNo(user.getPhoneNo());
		request.setCusId(finId);
		request.setCusName(finNm);
		request.setReason("������������");
		request.setSrcPdfPath(file.getFilePath());
		request.setDestPdfPath(file.getFilePath());
		request.setKey("����");
		request.setSignPage(Integer.parseInt("0"));
		request.setKeyIndex("1");
		request.setSealCode(sealCode);
		BaseResponse response = this.cfcaSignTxService.sign(request);
		if (!response.isSuccess()) {
			throw new UnCheckMsgException("������������������cfca��" + response.getMessage());
		}
	}
}
